import csv

import requests
import unittest
from ddt import ddt, data


@ddt
class A3mall(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')

    def tearDown(self) -> None:
        print('end case')

    token = None

    # 登录
    def test_A3mall_02(self):
        login = requests.post(url='http://10.59.9.14:7003/api/public/login',
                              json={"username": "18185603008", "password": "123456"})
        print(login.json())
        A3mall.token = login.json()['data']['token']
        print(A3mall.token)

    # 我的
    def test_A3mall_03(self):
        info = requests.get(url='http://10.59.9.14:7003/api/ucenter/info',
                            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(info.json())

    spid = None

    # 搜索
    def test_A3mall_04(self):
        search = requests.get(
            url='http://10.59.9.14:7003/api/search/list?page=1&keywords=%E5%B0%8F%E7%B1%B3&type=0&sort=1')

        print(search.json())
        # 提取商品id
        A3mall.spid = search.json()['data']['list'][0]['id']
        print('商品id:', A3mall.spid)

    # 商品详情
    def test_A3mall_05(self):
        view = requests.get(url='http://10.59.9.14:7003/api/goods/view?id=' + str(A3mall.spid))
        print(view.json())

    filePath = 'data.csv'

    def get_csv(self, filePath):
        datalist = []
        with open(filePath, 'r') as f:
            datas = csv.reader(f)
            next(datas)
            for i in datas:
                datalist.append(i)
        return datalist

    # 添加购物车
    @data(*get_csv(None, filePath))
    def test_A3mall_06(self, csv_data):
        num = csv_data
        print(num[0])
        add = requests.post(url='http://10.59.9.14:7003/api/cart/add',
                            headers={'auth-token': 'Bearer ' + A3mall.token},
                            json={"id": A3mall.spid, "sku_id": "", "num": num[0]})
        print(add.json())


if __name__ == '__main__':
    unittest.main()
